﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using QLVMBAYDatacess;
using QLVMBAYDomain;

namespace QLVMBAY.Admin
{
    public partial class QLVeGhe : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrid();
            }
        }
        private void LoadGrid()
        {
            grdVeGhe.DataSource = VeGheDA.Inst.GetAll(true);
            grdVeGhe.DataBind();
            if (grdVeGhe.PageCount <= 1)
            {
                grdVeGhe.PagerStyle.Visible = false;
            }
        }
        void Load_dlLoaiGhe()
        {
            List<LoaiGhe > list = new List<LoaiGhe>();
            list = LoaiGheDA.Inst.GetAll(true);
            dlLoaiGhe.DataSource = list;
            dlLoaiGhe.DataTextField = "TenLoaiGhe";
            dlLoaiGhe.DataValueField = "MaloaiGhe";
            dlLoaiGhe.DataBind();
            dlLoaiGhe.Items.Insert(0, new ListItem("Chọn", "0"));
        }
        void Load_dlLoaiVe()
        {
            List<LoaiVe > list = new List<LoaiVe>();
            list = LoaiVeDA.Inst.GetAll(true);
            dlLoaiVe.DataSource = list;
            dlLoaiVe.DataTextField = "TenLoaiVe";
            dlLoaiVe.DataValueField = "MaloaiVe";
            dlLoaiVe.DataBind();
            dlLoaiVe.Items.Insert(0, new ListItem("Chọn", "0"));
        }
        
        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            pnView.Visible = false;
            Insert = true;
            txtMa.Visible = false;
            lblMa.Visible = false;
            txtDienGiai.Text ="";            
            Load_dlLoaiGhe();
            Load_dlLoaiVe();
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            LoadGrid();
        } 

        protected void Update_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Ve_Ghe  GV = new Ve_Ghe ();
                GV.ItemLoaiGhe = new LoaiGhe() { MaLoaiGhe=dlLoaiGhe.SelectedValue };
                GV.ItemLoaiVe = new LoaiVe() { MaLoaiVe=dlLoaiVe.SelectedValue };
                GV.DienGiai = txtDienGiai.Text.ToString();
                
                
                if (Insert == true)
                {
                    VeGheDA.Inst.Insert(GV);
                }
                else
                {
                    GV.MaGheVe = int.Parse(txtMa.Text);
                    VeGheDA.Inst.Update(GV);
                }
                LoadGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                Insert = false;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        protected void grdVeGhe_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var itemGV = VeGheDA.Inst.GetByPrimaryKey(false, e.Item.Cells[0].Text);
            switch (e.CommandName)
            {
                case "Edit":
                    Insert = false;
                    Load_dlLoaiGhe();
                    dlLoaiGhe.SelectedValue = itemGV.ItemLoaiGhe.MaLoaiGhe.ToString();
                    Load_dlLoaiVe();
                    dlLoaiVe.SelectedValue = itemGV.ItemLoaiVe.MaLoaiVe.ToString();
                    txtDienGiai.Text = itemGV.DienGiai;
                    txtMa.Text = itemGV.MaGheVe.ToString();
                    txtMa.Visible = true;
                    txtMa.ReadOnly = true;
                    lblMa.Visible = true;
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;

                case "Delete":
                    VeGheDA.Inst.Delete(itemGV);
                    LoadGrid();
                    break;
            }
        }

        protected void grdVeGhe_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                var GV = (Ve_Ghe )e.Item.DataItem;
                var lbl1 = e.Item.FindControl("lblTenLG") as Label;
                lbl1.Text = GV.ItemLoaiGhe.TenLoaiGhe ;                    
                var lbl2 = e.Item.FindControl("lblTenLV") as Label;
                lbl2.Text = GV.ItemLoaiVe.TenLoaiVe;                                   
            }
        }

        protected void grdVeGhe_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdVeGhe.CurrentPageIndex = e.NewPageIndex;
            LoadGrid();
        }
    }
}